#include "displaywindow.h"
DisplayWindow::DisplayWindow(QWidget *parent) : QMainWindow (parent){
    setupUI();
    // 读取gsetting配置，任一项变化时实时显示不同内容
    gsettings = new QGSettings("com.mytest.gsettings");
    connect(gsettings, &QGSettings::changed, this, [this](const QString& key){
        if(key == "name" ||key == "age" ||key == "cid" ||key == "phone" ||key == "home" ||key == "tem"){
            updateGSettings();
        }
    });
}

void DisplayWindow::setupUI(){
    this->setWindowTitle("gsettings-display");
    this->resize(400, 300);
    name = new QLabel("姓名");
    nameInput = new QLineEdit(this);
    age = new QLabel("年龄");
    ageInput = new QLineEdit(this);
    phone = new QLabel("电话");
    phoneInput = new QLineEdit(this);
    cid = new QLabel("身份证号");
    cidInput = new QLineEdit(this);
    home = new QLabel("住址");
    homeInput = new QLineEdit(this);
    tem = new QLabel("体温");
    temInput = new QLineEdit(this);
    QFormLayout* formLayout = new QFormLayout();
    formLayout->addRow(name, nameInput);
    formLayout->addRow(age, ageInput);
    formLayout->addRow(phone, phoneInput);
    formLayout->addRow(cid, cidInput);
    formLayout->addRow(home, homeInput);
    formLayout->addRow(tem, temInput);
    QHBoxLayout* headLayout = new QHBoxLayout();
    QVBoxLayout* mainLayout = new QVBoxLayout();
    mainLayout->addLayout(headLayout);
    mainLayout->addLayout(formLayout);

    QWidget* centralWidget = new QWidget(this);
    centralWidget->setLayout(mainLayout);
    setCentralWidget(centralWidget);
}

void DisplayWindow::updateGSettings(){
    nameInput->setText(gsettings->get("name").toString());
    ageInput->setText(gsettings->get("age").toString());
    phoneInput->setText(gsettings->get("phone").toString());
    cidInput->setText(gsettings->get("cid").toString());
    homeInput->setText(gsettings->get("home").toString());
    temInput->setText(gsettings->get("tem").toString());
}
